package com.novacloud.fresh.password;

import com.novacloud.fresh.model.login.DUser;
import org.jasig.cas.authentication.handler.PasswordEncoder;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * @param
 * @author <a href="mailto:huzhanjun@novacloud.com">huzj</a>
 * @version : 1.0
 * @date 2015-12-15 12:43
 */
public class FreshPasswordEncoder implements PasswordEncoder {

    private final static Log logger = LogFactory
            .getLog(DUser.class);

    //
    private final static String ALGORITHM = "MD5";

    /**
     *
     */
//    private Md5Encoder() {
//
//    }

    public FreshPasswordEncoder() {
    }

    @Override
    public String encode(String password) {
        MessageDigest messageDigest = null;

        try {
            messageDigest = MessageDigest.getInstance(ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            logger.error(e.getMessage(), e);
        }

        byte[] digest = messageDigest.digest(password.getBytes());

        if (logger.isDebugEnabled()) {
            logger.debug("encode password is:"
                    + new String(Hex.encodeHex(digest)));
        }

        return new String(Hex.encodeHex(digest));
    }

}
